package com.edais.actions;

import com.edais.service.CaptchaService;
import com.yunzhi.domain.ResultConstant;
import com.yunzhi.util.RequestUtil;
import com.yunzhi.util.ResponseUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.Map;

@Controller
@RequestMapping("/captcha")

public class CaptchaAction  implements Serializable{
	private static final Log logger = LogFactory.getLog(CaptchaAction.class);

	@Autowired
	private CaptchaService captchaService;

	/** 获取验证码
	 * @param request
	 * @return response
	 * @throws Exception */
	@RequestMapping(value = "/getCaptcha")
	public void getMyInvitationCd(HttpServletRequest request, HttpServletResponse response){

		//验证签名
		boolean valid_sign = RequestUtil.validSign(request);
		if(!valid_sign){
			ResponseUtil.write(response,ResultConstant.INVALID_SIGN);
			return;
		}

		// 解密请求参数
//		Map<String, String> paramsMap = RequestUtil.decodeData(request);
		Map<String, String> paramsMap = null;

		// 用户id
		String userId = paramsMap.get("userId");
		// 验证码类别：设置汇款卡号(RECEPAY_CARD), 修改密码(MODIFY_PASSWORD)
		String type = paramsMap.get("type");

		//校验用户id
		if (StringUtils.isBlank(userId) || null == type) {
			logger.debug("/captcha/getCaptcha 传入参数为空");
			ResponseUtil.write(response,ResultConstant.FAILED);
			return;
		}
		try{
			// 发送验证码
			ResponseUtil.write(response,this.captchaService.getCaptcha(request.getSession(), userId, type));
		} catch (Exception e) {
			logger.error("captcha/captcha 异常:" + e.getCause());
			ResponseUtil.write(response,ResultConstant.FAILED);
		}		
	}
}
